package com.suray.system.run.agv;

import com.alibaba.fastjson.JSON;
import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.system.run.agv.bean.PermitInBean;
import com.suray.system.run.agv.bean.StationRunBean;
import com.suray.system.run.agv.bean.UpperResponse;
import com.suray.wcs.service.config.properties.WcsProperties;
import com.suray.wcs.service.core.util.HttpClientSpring;
import com.suray.wcs.service.enums.NetAddressType;
import com.suray.wcs.service.enums.NotificationTypeEnum;
import com.suray.wcs.service.system.service.impl.NetAddressManageDBServiceImpl;
import com.suray.wcs.service.util.WcsUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 向上层系统发送信息
 */
public class ToUpper {
    private static Logger log = LoggerFactory.getLogger(ToUpper.class);

    private static NetAddressManageDBServiceImpl netAddressManageDBService =
            SpringContextHolder.getBean(NetAddressManageDBServiceImpl.class);

    final public static String sendTaskGetPutPath = "/agv/applyGetPut";
    final public static String sendTaskRunPath = "/agv/stationRun";

    private static String getUpperStrataPort() {
        return netAddressManageDBService.findNetAddressByType(NetAddressType.UPPER_STRATA).get(0).getPort();
    }

    private static String getUpperStrataIp() {
        return netAddressManageDBService.findNetAddressByType(NetAddressType.UPPER_STRATA).get(0).getIp();
    }


    /**
     * Agv申请取货或者放货
     * @param permit
     * @return
     */
    public static boolean sendTaskApplyGetPut(PermitInBean permit){
        LoggerUtil.info("Agv申请取货或者放货=>" + JSON.toJSONString(permit), ToUpper.class);
        String result = StringUtils.EMPTY;
        for (int i = 0; i < WcsProperties.ATTEMPT_NUM_3; i++) {
            try {
                result = HttpClientSpring.sendPostRequest(WcsFuncUtil.getHttpUrl(getUpperStrataIp(), getUpperStrataPort(), sendTaskGetPutPath),
                        JSON.toJSONString(permit));
                if (StringUtils.isNotBlank(result)) {
                    break;
                }
            } catch (Exception e) {
                LoggerUtil.info("Agv申请取货或者放货 接口异常：" + e.getMessage() + e, ToUpper.class);
                WcsFuncUtil.delay(1000);
            }
        }
        LoggerUtil.info("Agv申请取货或者放货 return data:" + result, ToUpper.class);
        try {
            if (StringUtils.isBlank(result)) {
                return false;
            }
            UpperResponse UpperResponse = JSON.parseObject(result, UpperResponse.class);

            Integer returnStatus = Integer.valueOf(UpperResponse.getReturnStatus());
            if (returnStatus == WcsProperties.SUCCESS) {
                LoggerUtil.info("Agv申请取货或者放货 " + permit + "成功", ToUpper.class);
                return true;
            } else if (returnStatus == WcsProperties.FAIL) {
                LoggerUtil.error("Agv申请取货或者放货 " + permit + "失败！原因-->" + UpperResponse.getReturnInfo() + ", "
                        + UpperResponse.getMsgTime(), ToUpper.class);
                WcsUtil.notification("Agv申请取货或者放货失败：" + permit.getAgvApoint() + permit.getApplyTime(), NotificationTypeEnum.ERROR);
                return false;
            } else {
                LoggerUtil.error("Agv申请取货或者放货 状态码不存在,接收到为:" + returnStatus, ToUpper.class);
                return false;
            }
        } catch (Exception e) {
            WcsFuncUtil.exceptionLog(e, ToUpper.class);
            LoggerUtil.error("Agv申请取货或者放货 解析返回值出现异常！" + e.getMessage() + e, ToUpper.class);
            return false;
        }
    }

    /**
     * Agv申请输送线运行
     * @param stationRun
     * @return
     */
    public static boolean sendTaskStationRun(StationRunBean stationRun){
        LoggerUtil.info("Agv申请输送线运行=>" + JSON.toJSONString(stationRun), ToUpper.class);
        String result = StringUtils.EMPTY;
        for (int i = 0; i < WcsProperties.ATTEMPT_NUM_3; i++) {
            try {
                result = HttpClientSpring.sendPostRequest(WcsFuncUtil.getHttpUrl(getUpperStrataIp(), getUpperStrataPort(), sendTaskRunPath),
                        JSON.toJSONString(stationRun));
                if (StringUtils.isNotBlank(result)) {
                    break;
                }
            } catch (Exception e) {
                LoggerUtil.info("Agv申请输送线运行 接口异常：" + e.getMessage() + e, ToUpper.class);
                WcsFuncUtil.delay(1000);
            }
        }
        LoggerUtil.info("Agv申请输送线运行 return data:" + result, ToUpper.class);
        try {
            if (StringUtils.isBlank(result)) {
                return false;
            }
            UpperResponse UpperResponse = JSON.parseObject(result, UpperResponse.class);

            Integer returnStatus = Integer.valueOf(UpperResponse.getReturnStatus());
            if (returnStatus == WcsProperties.SUCCESS) {
                LoggerUtil.info("Agv申请输送线运行" + stationRun + "下发成功", ToUpper.class);
                return true;
            } else if (returnStatus == WcsProperties.FAIL) {
                LoggerUtil.error("Agv申请输送线运行" + stationRun + "下发失败！原因-->" + UpperResponse.getReturnInfo() + ", "
                        + UpperResponse.getMsgTime(), ToUpper.class);
                WcsUtil.notification("Agv申请输送线运行 失败：" + UpperResponse.getReturnInfo(), NotificationTypeEnum.ERROR);
                return false;
            } else {
                LoggerUtil.error("Agv申请输送线运行 状态码不存在,接收到为:" + returnStatus, ToUpper.class);
                return false;
            }
        } catch (Exception e) {
            WcsFuncUtil.exceptionLog(e, ToUpper.class);
            LoggerUtil.error("Agv申请输送线运行 解析返回值出现异常！" + e.getMessage() + e, ToUpper.class);
            return false;
        }
    }

}




